package cn.iocoder.yudao.module.wine.service.stockinfo;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.wine.controller.app.stockinfo.vo.AppStockInfoListReqVO;
import cn.iocoder.yudao.module.wine.controller.app.stockinfo.vo.AppStockInfoRespVO;
import cn.iocoder.yudao.module.wine.controller.app.stockinfo.vo.AppStockInfoSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.stockinfo.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.stockinfo.StockInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.stockinfo.StockInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 出入库信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StockInfoServiceImpl implements StockInfoService {

    @Resource
    private StockInfoMapper stockInfoMapper;

    @Override
    public Long createStockInfo(StockInfoSaveReqVO createReqVO) {
        // 插入
        StockInfoDO stockInfo = BeanUtils.toBean(createReqVO, StockInfoDO.class);
        stockInfoMapper.insert(stockInfo);
        // 返回
        return stockInfo.getId();
    }

    @Override
    public void updateStockInfo(StockInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateStockInfoExists(updateReqVO.getId());
        // 更新
        StockInfoDO updateObj = BeanUtils.toBean(updateReqVO, StockInfoDO.class);
        stockInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteStockInfo(Long id) {
        // 校验存在
        validateStockInfoExists(id);
        // 删除
        stockInfoMapper.deleteById(id);
    }

    private void validateStockInfoExists(Long id) {
        if (stockInfoMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public StockInfoDO getStockInfo(Long id) {
        return stockInfoMapper.selectById(id);
    }

    @Override
    public PageResult<StockInfoRespVO> getStockInfoPage(StockInfoPageReqVO pageReqVO) {
        return stockInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public Long appCreateStockInfo(AppStockInfoSaveReqVO createReqVO) {
        createReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId());
        // 插入
        StockInfoDO stockInfo = BeanUtils.toBean(createReqVO, StockInfoDO.class);
        stockInfoMapper.insert(stockInfo);
        // 返回
        return stockInfo.getId();
    }

    @Override
    public Boolean appUpdateStockInfo(AppStockInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateStockInfoExists(updateReqVO.getId());
        // 更新
        StockInfoDO updateObj = BeanUtils.toBean(updateReqVO, StockInfoDO.class);
        return stockInfoMapper.updateById(updateObj) > 0;
    }

    @Override
    public List<AppStockInfoRespVO> getStockInfoList(AppStockInfoListReqVO reqVO) {
        return stockInfoMapper.getStockInfoList(reqVO);
    }

}
